home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / python2.4 / lib-tk / tkSimpleDialog.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  8.0 KB  |  271 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. '''Dialog boxes
  5.  
  6. This module handles dialog boxes. It contains the following
  7. public symbols:
  8.  
  9. Dialog -- a base class for dialogs
  10.  
  11. askinteger -- get an integer from the user
  12.  
  13. askfloat -- get a float from the user
  14.  
  15. askstring -- get a string from the user
  16. '''
  17. from Tkinter import *
  18. import os
  19.  
  20. class Dialog(Toplevel):
  21.     '''Class to open dialogs.
  22.  
  23.     This class is intended as a base class for custom dialogs
  24.     '''
  25.     
  26.     def __init__(self, parent, title = None):
  27.         '''Initialize a dialog.
  28.  
  29.         Arguments:
  30.  
  31.             parent -- a parent window (the application window)
  32.  
  33.             title -- the dialog title
  34.         '''
  35.         Toplevel.__init__(self, parent)
  36.         self.transient(parent)
  37.         if title:
  38.             self.title(title)
  39.         
  40.         self.parent = parent
  41.         self.result = None
  42.         body = Frame(self)
  43.         self.initial_focus = self.body(body)
  44.         body.pack(padx = 5, pady = 5)
  45.         self.buttonbox()
  46.         self.wait_visibility()
  47.         self.grab_set()
  48.         if not self.initial_focus:
  49.             self.initial_focus = self
  50.         
  51.         self.protocol('WM_DELETE_WINDOW', self.cancel)
  52.         if self.parent is not None:
  53.             self.geometry('+%d+%d' % (parent.winfo_rootx() + 50, parent.winfo_rooty() + 50))
  54.         
  55.         self.initial_focus.focus_set()
  56.         self.wait_window(self)
  57.  
  58.     
  59.     def destroy(self):
  60.         '''Destroy the window'''
  61.         self.initial_focus = None
  62.         Toplevel.destroy(self)
  63.  
  64.     
  65.     def body(self, master):
  66.         '''create dialog body.
  67.  
  68.         return widget that should have initial focus.
  69.         This method should be overridden, and is called
  70.         by the __init__ method.
  71.         '''
  72.         pass
  73.  
  74.     
  75.     def buttonbox(self):
  76.         '''add standard button box.
  77.  
  78.         override if you do not want the standard buttons
  79.         '''
  80.         box = Frame(self)
  81.         w = Button(box, text = 'OK', width = 10, command = self.ok, default = ACTIVE)
  82.         w.pack(side = LEFT, padx = 5, pady = 5)
  83.         w = Button(box, text = 'Cancel', width = 10, command = self.cancel)
  84.         w.pack(side = LEFT, padx = 5, pady = 5)
  85.         self.bind('<Return>', self.ok)
  86.         self.bind('<Escape>', self.cancel)
  87.         box.pack()
  88.  
  89.     
  90.     def ok(self, event = None):
  91.         if not self.validate():
  92.             self.initial_focus.focus_set()
  93.             return None
  94.         
  95.         self.withdraw()
  96.         self.update_idletasks()
  97.         self.apply()
  98.         self.cancel()
  99.  
  100.     
  101.     def cancel(self, event = None):
  102.         if self.parent is not None:
  103.             self.parent.focus_set()
  104.         
  105.         self.destroy()
  106.  
  107.     
  108.     def validate(self):
  109.         '''validate the data
  110.  
  111.         This method is called automatically to validate the data before the
  112.         dialog is destroyed. By default, it always validates OK.
  113.         '''
  114.         return 1
  115.  
  116.     
  117.     def apply(self):
  118.         '''process the data
  119.  
  120.         This method is called automatically to process the data, *after*
  121.         the dialog is destroyed. By default, it does nothing.
  122.         '''
  123.         pass
  124.  
  125.  
  126.  
  127. class _QueryDialog(Dialog):
  128.     
  129.     def __init__(self, title, prompt, initialvalue = None, minvalue = None, maxvalue = None, parent = None):
  130.         if not parent:
  131.             import Tkinter
  132.             parent = Tkinter._default_root
  133.         
  134.         self.prompt = prompt
  135.         self.minvalue = minvalue
  136.         self.maxvalue = maxvalue
  137.         self.initialvalue = initialvalue
  138.         Dialog.__init__(self, parent, title)
  139.  
  140.     
  141.     def destroy(self):
  142.         self.entry = None
  143.         Dialog.destroy(self)
  144.  
  145.     
  146.     def body(self, master):
  147.         w = Label(master, text = self.prompt, justify = LEFT)
  148.         w.grid(row = 0, padx = 5, sticky = W)
  149.         self.entry = Entry(master, name = 'entry')
  150.         self.entry.grid(row = 1, padx = 5, sticky = W + E)
  151.         if self.initialvalue:
  152.             self.entry.insert(0, self.initialvalue)
  153.             self.entry.select_range(0, END)
  154.         
  155.         return self.entry
  156.  
  157.     
  158.     def validate(self):
  159.         import tkMessageBox
  160.         
  161.         try:
  162.             result = self.getresult()
  163.         except ValueError:
  164.             tkMessageBox.showwarning('Illegal value', self.errormessage + '\nPlease try again', parent = self)
  165.             return 0
  166.  
  167.         if self.minvalue is not None and result < self.minvalue:
  168.             tkMessageBox.showwarning('Too small', 'The allowed minimum value is %s. Please try again.' % self.minvalue, parent = self)
  169.             return 0
  170.         
  171.         if self.maxvalue is not None and result > self.maxvalue:
  172.             tkMessageBox.showwarning('Too large', 'The allowed maximum value is %s. Please try again.' % self.maxvalue, parent = self)
  173.             return 0
  174.         
  175.         self.result = result
  176.         return 1
  177.  
  178.  
  179.  
  180. class _QueryInteger(_QueryDialog):
  181.     errormessage = 'Not an integer.'
  182.     
  183.     def getresult(self):
  184.         return int(self.entry.get())
  185.  
  186.  
  187.  
  188. def askinteger(title, prompt, **kw):
  189.     '''get an integer from the user
  190.  
  191.     Arguments:
  192.  
  193.         title -- the dialog title
  194.         prompt -- the label text
  195.         **kw -- see SimpleDialog class
  196.  
  197.     Return value is an integer
  198.     '''
  199.     d = _QueryInteger(title, prompt, **kw)
  200.     return d.result
  201.  
  202.  
  203. class _QueryFloat(_QueryDialog):
  204.     errormessage = 'Not a floating point value.'
  205.     
  206.     def getresult(self):
  207.         return float(self.entry.get())
  208.  
  209.  
  210.  
  211. def askfloat(title, prompt, **kw):
  212.     '''get a float from the user
  213.  
  214.     Arguments:
  215.  
  216.         title -- the dialog title
  217.         prompt -- the label text
  218.         **kw -- see SimpleDialog class
  219.  
  220.     Return value is a float
  221.     '''
  222.     d = _QueryFloat(title, prompt, **kw)
  223.     return d.result
  224.  
  225.  
  226. class _QueryString(_QueryDialog):
  227.     
  228.     def __init__(self, *args, **kw):
  229.         if kw.has_key('show'):
  230.             self._QueryString__show = kw['show']
  231.             del kw['show']
  232.         else:
  233.             self._QueryString__show = None
  234.         _QueryDialog.__init__(self, *args, **kw)
  235.  
  236.     
  237.     def body(self, master):
  238.         entry = _QueryDialog.body(self, master)
  239.         if self._QueryString__show is not None:
  240.             entry.configure(show = self._QueryString__show)
  241.         
  242.         return entry
  243.  
  244.     
  245.     def getresult(self):
  246.         return self.entry.get()
  247.  
  248.  
  249.  
  250. def askstring(title, prompt, **kw):
  251.     '''get a string from the user
  252.  
  253.     Arguments:
  254.  
  255.         title -- the dialog title
  256.         prompt -- the label text
  257.         **kw -- see SimpleDialog class
  258.  
  259.     Return value is a string
  260.     '''
  261.     d = _QueryString(title, prompt, **kw)
  262.     return d.result
  263.  
  264. if __name__ == '__main__':
  265.     root = Tk()
  266.     root.update()
  267.     print askinteger('Spam', 'Egg count', initialvalue = 12 * 12)
  268.     print askfloat('Spam', 'Egg weight\n(in tons)', minvalue = 1, maxvalue = 100)
  269.     print askstring('Spam', 'Egg label')
  270.  
  271.